//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lyq.bean.mySql;

import com.lyq.bean.Student;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class SeekS_Sql {
    public SeekS_Sql() {
    }

    public List<Student> seekS_Sql(String select, String seek, String name, List<Student> list) {
        (new MysqlLinking()).getLink();
        Connection conn = MysqlLinking.conn;
        if (conn != null) {
            try {
                String sql = "";
                PreparedStatement ps = null;
                if (seek.equals("模糊查询")) {
                    sql = "select * from student where " + select + " like '%" + name + "%'";
                    ps = conn.prepareStatement(sql);
                } else if (seek.equals("精确查询")) {
                    sql = "select * from student where " + select + "=?";
                    ps = conn.prepareStatement(sql);
                    ps.setString(1, name);
                }

                ResultSet rs = ps.executeQuery();

                while(rs.next()) {
                    Student s = new Student();
                    s.setNo(rs.getString("S_No"));
                    s.setName(rs.getString("S_Name"));
                    s.setSex(rs.getString("S_Sex"));
                    s.setAge(rs.getString("S_Age"));
                    s.setCollage(rs.getString("S_Collage"));
                    s.setDepartment(rs.getString("S_Department"));
                    // 获取GPA字段
                    s.setGpa(rs.getDouble("gpa"));
                    list.add(s);
                }
            } catch (Exception var10) {
                Exception e = var10;
                e.printStackTrace();
            }
        } else {
            System.out.println("数据库连接错误！");
        }

        return list;
    }



    public List<Student> getStudentsByMajorAndYear(int majorId, int yearId, List<Student> list) {
        (new MysqlLinking()).getLink();
        Connection conn = MysqlLinking.conn;

        if (conn != null) {
            try {
                String sql = "SELECT * FROM student WHERE major_id = ? AND year_id = ?";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setInt(1, majorId);
                ps.setInt(2, yearId);

                ResultSet rs = ps.executeQuery();

                while (rs.next()) {
                    Student s = new Student();
                    s.setNo(rs.getString("S_No"));
                    s.setName(rs.getString("S_Name"));
                    s.setSex(rs.getString("S_Sex"));
                    s.setAge(rs.getString("S_Age"));
                    s.setCollage(rs.getString("S_Collage"));
                    s.setDepartment(rs.getString("S_Department"));

                    s.setMajorId(rs.getInt("major_id"));
                    s.setYearId(rs.getInt("year_id"));
                    list.add(s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("数据库连接错误！");
        }

        return list;
    }
}
